﻿/*==========================
 * Author: TLTHau
 * Create Date: 2013/06/07
 ==========================*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SketchTour.Models;
using Ivs.Core.Mapping;
using SketchTour.Paging;
using SketchTour.Common;
using Ivs.Core.Common.Utilities;
using System.Collections;
using SketchTour.DAL;

namespace SketchTour.Controllers.Master
{
    public class MSGF00Controller : BaseController
    {
        #region Property

        int RecordsPerPage = Utilities.GetNumberPerPage();
        private MSGF00DAL service = new MSGF00DAL();

        #endregion Property

        #region Home

        public ActionResult Index()
        {
            MSGF00Models Model = null;
            //xoa thong tin session tu cac trang khac
            if (Request.UrlReferrer != null)
            {
                if (Request.Url.ToString() != Request.UrlReferrer.ToString())
                {
                    Session.Remove("SEARCH_CONDITION");
                }
                else
                {
                    Hashtable htbCondition = (Hashtable)Session["SEARCH_CONDITION"];
                    //Neu da co dieu kien search thi search lai du lieu
                    if (!Utilities.isNullOrEmpty(htbCondition) && !Utilities.isNullOrEmpty(htbCondition[CData.ScreenID.MSGF00]))
                    {
                        Model = (MSGF00Models)htbCondition[CData.ScreenID.MSGF00];
                    }
                }
            }
            else
            {
                Session.Remove("SEARCH_CONDITION");
            }

            //search du lieu neu Model khong rong
            IEnumerable<MSGF00Models> data = new List<MSGF00Models>();
            if (!Utilities.isNullOrEmpty(Model))
            {
                int result = service.Search(Model, out data);
            }
            var list = data.ToPagedList(1, RecordsPerPage);
            //Create dropdownlist City save to  ViewBag.CityCode
            ViewBag.CityCode = new SelectList(service.SearchCity(), MSGF00Key.City, MSGF00Key.City);          
            return View(list);
        }

        [HttpPost]
        public ActionResult Index(int? page, MSGF00Models Model)
        {
            //Kiem tra session luu dieu kien search
            if (!Utilities.isNullOrEmpty(Session["SEARCH_CONDITION"]))
            {
                Hashtable htbCondition = (Hashtable)Session["SEARCH_CONDITION"];
                //khi phan trang se su dung lai dieu kien search
                //khi page.HasValue la su dung phan trang
                if (!Utilities.isNullOrEmpty(htbCondition[CData.ScreenID.MSGF00]) && page.HasValue)
                {
                    Model = (MSGF00Models)htbCondition[CData.ScreenID.MSGF00];
                }
                //khi search, page = null
                //luu lai dieu kien search moi
                else
                {
                    htbCondition[CData.ScreenID.MSGF00] = Model;
                    Session["SEARCH_CONDITION"] = htbCondition;
                }

            }
            //luu lai dieu kien search moi
            else
            {
                Hashtable htbCondition = new Hashtable();
                htbCondition[CData.ScreenID.MSGF00] = Model;
                Session["SEARCH_CONDITION"] = htbCondition;
            }
            IEnumerable<MSGF00Models> data;
            int result = service.Search(Model, out data);
            int pageSize = RecordsPerPage;
            int pageNumber = (page ?? 1);
            var list = data.ToPagedList(pageNumber, pageSize);
            //Create dropdownlist City save to  ViewBag.CityCode
            ViewBag.CityCode = new SelectList(service.SearchCity(), MSGF00Key.City, MSGF00Key.City);
            return PartialView(CData.ScreenID.MSGF00Partial, list);
        }

        #endregion Home

        #region Create
        //
        // GET: /MSGF00/Add
        public ActionResult Add()
        {
            //Create dropdownlist City save to  ViewBag.CityCode
            ViewBag.CityCode = new SelectList(service.SearchCity(), MSGF00Key.City, MSGF00Key.City);
            return View();
        }

        //
        // POST: /MSGF00/Add
        [HttpPost]
        public ActionResult Add(MSGF00Models s)
        {
            if (ModelState.IsValid)
            {
                //Call fuction insert
                int result = service.Insert(s);
                //Create message to display
                TempData[CData.MsgContent] = CMessage.DisplayMessageInserted(result);
                return RedirectToAction("Index");
            }
            ViewBag.CityCode = new SelectList(service.SearchCity(), MSGF00Key.City, MSGF00Key.City);
            return View(s);
        }
        #endregion Create

        #region Edit
        //
        // GET: /Default1/Edit/5

        public ActionResult Edit(string idEdit)
        {
            MSGF00Models s = null;
            if (idEdit != null && !String.IsNullOrEmpty(idEdit))
            {
                try
                {
                    string[] sa = idEdit.Split(';');
                    //Remove string unnecessary
                    int id = Utilities.parseInt(sa[0].Replace("IdDetail_", string.Empty));
                    s = service.Search(id);
                }
                catch { }
            }
            if (s == null)
            {
                return HttpNotFound();
            }
            ViewBag.CityCode = new SelectList(service.SearchCity(), MSGF00Key.City, MSGF00Key.City, s.CityCode);
            return View(s);
        }

        //
        // POST: /Default1/Edit/5

        [HttpPost]
        public ActionResult Edit(MSGF00Models s)
        {
            //Check Model IsValid
            if (ModelState.IsValid)
            {
                //Call fuction Update
                int result = service.Update(s);
                //Create message to display
                TempData[CData.MsgContent] = CMessage.DisplayMessageUpdated(result);
                return RedirectToAction("Index");
            }
            ViewBag.CityCode = new SelectList(service.SearchCity(), MSGF00Key.City, MSGF00Key.City, s.CityCode);            
            return View(s);
        }
        #endregion Edit

        #region Delete
        //
        // POST: /Default1/Delete/5

        [HttpPost]
        public ActionResult Delete(string idDelete)
        {
            //Check string idDelete
            if (idDelete != null && !String.IsNullOrEmpty(idDelete))
            {
                //Call fuction Delete
                int result = DeleteAll(idDelete);
                //Create message to display
                TempData[CData.MsgContent] = CMessage.DisplayMessageDeleted(result);
            }
            return RedirectToAction("Index");
        }
        private int DeleteAll(string idDelete)
        {
            //idDelete can is one or much id
            string[] s = idDelete.Split(';');
            List<int> listID = new List<int>();
            for (int i = 0; i < s.Count() - 1; i++)
            {
                //Remove string unnecessary
                listID.Add(Utilities.parseInt(s[i].Replace("IdDetail_", string.Empty)));
            }
            return service.Delete(listID);
        }
        #endregion Delete

    }
}
